package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MemberMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@DubboService
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member addIsNotEx(Member member) {
        //查询是否存在
        Member member1 = new Member();
        member1.setPhoneNumber(member.getPhoneNumber());
        Member member2 = memberMapper.selectOne(member1);
        if (member2 == null) {
            //不存在--》添加
            int i = memberMapper.add(member);
        }
        //存在--》更新
        int i = memberMapper.updateByPrimaryKeySelective(member);
        return member;
    }


    @Override
    public List<Map<String, Object>> countBySex() {
        return memberMapper.countBySex();
    }

    @Override
    public Member findById(Integer id) {
        return  memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> monthList) {
        List<Integer> resultList=new ArrayList<>();
        for (String month: monthList) {
            String endDate=month+".31";
            Long count = memberMapper.countByRegTimeBefore(endDate);
            resultList.add(count.intValue());
        }
        return resultList;
    }

    //    ，例如0-18、18-30、30-45、45以上
    @Override
    public List<Map<String, Object>> countByAgeRange() {
        List<Map<String, Object>> list = memberMapper.countByRegAge();
//        Integer count=memberDao.countByRegAge(String startDate,String endDate);
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
            //获取查询条件
            String queryString = queryPageBean.getQueryString();
            //分页查询起始
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //获取example对象
            Example example = new Example(Member.class);
            Example.Criteria criteria = example.createCriteria();
            //判断查询条件是否为空,模糊查询
            if (!StringUtils.isEmpty(queryString)) {
                criteria.orLike("fileNumber", "%" + queryString + "%");
                criteria.orLike("name", "%" + queryString + "%");
                criteria.orLike("phoneNumber", "%" + queryString + "%");
            }
            Page<Member> page = (Page<Member>) memberMapper.selectByExample(example);
            List<Member> members = page.getResult();
            long total = page.getTotal();
            return new PageResult(total, members);
    }

    @Override
    public void add(Member member) {
        member.setRegTime(new Date());
        memberMapper.insertSelective(member);
    }

    @Override
    public void delete(Integer id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void edit(Member member) {
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public List<Member> findAll() {
        return memberMapper.selectAll();
    }

    @Override
    public Member findByTele(String tele) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("phoneNumber",tele);
        Member member = memberMapper.selectOneByExample(example);
        return member;
    }
}
